package com.luocj.looook;

import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.luocj.looook.base.BaseActivity;
import com.luocj.looook.fragment.AFragment;
import com.luocj.looook.fragment.DragItemFragment;
import com.luocj.looook.fragment.DrawerFragment;
import com.luocj.looook.fragment.MEINVFragment;
import com.luocj.looook.fragment.NBAFragment;
import com.luocj.looook.fragment.NewsFragment;
import com.luocj.looook.fragment.TESTFragment;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private List<Fragment> mFragment = new ArrayList<>();

    //    private final String[] mTitles = {"新闻", "NBA", "美女", "抽屉","Test"};
    private final String[] mTitles = {"新闻", "NBA", "美女", "- -||", "抽屉", "拖拽Item", "Test"};
    private MyPagerAdapter mAdapter;
    private ViewPager viewPager;
    private SlidingTabLayout tablayout;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tablayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
    }

    @Override
    protected void initData() {
        mFragment.add(new NewsFragment());
        mFragment.add(new NBAFragment());
        mFragment.add(new MEINVFragment());
        mFragment.add(new AFragment());
        mFragment.add(new DrawerFragment());
        mFragment.add(new DragItemFragment());
        mFragment.add(new TESTFragment());
//        mFragment.add(new TEST1Fragment());

        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mAdapter);
        tablayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(4);

    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        public MyPagerAdapter(FragmentManager supportFragmentManager) {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragment.get(position);
        }

        @Override
        public int getCount() {
            return mFragment.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int i = 0; i < grantResults.length; i++) {
            Logger.i(permissions.toString() + grantResults[0] + "------>");
        }
        if (requestCode == 1001 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            Logger.i("111");
        } else {
            Logger.i("123");
        }
    }
}
